package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.MyMap;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

@SuppressWarnings("rawtypes")
public class MyMapTest {
    private String key1 = "a";
    private String key2 = "a b c";
    private String key3 = "";
    private String key4 = null;

    @SuppressWarnings("unused")
    @Test
    public void testMyMap() {
        MyMap<String> s = new MyMap<String>();
        MyMap<Object> o = new MyMap<Object>();
        MyMap<MyMap> m = new MyMap<MyMap>();
    }

    @Test
    public void testSet() throws UniversityFailure, UniversityWarning {
        MyMap<String> s = new MyMap<String>();
        s.set(key1, "1");
        s.set(key2, "2");
        s.set(key3, "3");
        try {
            s.set(key4, "4");
            fail("Missed " + ErrorCodes.MAP_KEY_IS_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.MAP_KEY_IS_NULL, e.getMessage());
        }
        s.set(key1, "1");
        s.set(key2, "2");
        s.set(key3, "3");

        MyMap<Object> o = new MyMap<Object>();
        o.set(key1, new Object());
        o.set(key2, new Object());
        o.set(key3, new Object());
        o.set(key1, new Object());
        o.set(key2, new Object());
        o.set(key3, new Object());

        MyMap<MyMap> m = new MyMap<MyMap>();
        m.set(key1, new MyMap());
        m.set(key2, new MyMap());
        m.set(key3, new MyMap());
        m.set(key1, new MyMap());
        m.set(key2, new MyMap());
        m.set(key3, new MyMap());

    }

    @Test
    public void testGet() throws UniversityFailure, UniversityWarning {
        MyMap<String> s = new MyMap<String>();
        s.set(key1, "1");
        s.set(key2, "2");
        try {
            s.get(key3);
            fail("Missed " + ErrorCodes.MAP_KEY_NOT_FOUND);
        } catch (Exception e) {
            assertEquals(ErrorCodes.MAP_KEY_NOT_FOUND + key3,
                    e.getMessage());
        }
        s.set(key3, "3");
        assertEquals("1", s.get(key1));
        assertEquals("2", s.get(key2));
        assertEquals("3", s.get(key3));
        try {
            s.get(key4);
            fail("Missed " + ErrorCodes.MAP_KEY_IS_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.MAP_KEY_IS_NULL, e.getMessage());
        }

        MyMap<Object> o = new MyMap<Object>();
        o.set(key1, new String("1"));
        o.set(key2, new String("2"));
        o.set(key3, new String("3"));
        assertEquals("1", o.get(key1));
        assertEquals("2", o.get(key2));
        assertEquals("3", o.get(key3));

    }

    @Test
    public void testRemove() throws UniversityFailure, UniversityWarning {
        MyMap<String> s = new MyMap<String>();
        s.set(key1, "1");
        s.set(key2, "2");
        try {
            s.remove(key3);
            fail("Missed " + ErrorCodes.ARRAY_INDEX_IS_OUT_OF_BOUNDARIES);
        } catch (Exception e) {
            assertEquals(ErrorCodes.ARRAY_INDEX_IS_OUT_OF_BOUNDARIES,
                    e.getMessage());
        }
        s.set(key3, "3");
        s.remove(key1);
        s.remove(key2);
        s.remove(key3);
        try {
            s.remove(key4);
            fail("Missed " + ErrorCodes.MAP_KEY_IS_NULL);
        } catch (Exception e) {
            assertEquals(ErrorCodes.MAP_KEY_IS_NULL, e.getMessage());
        }
    }

    @Test
    public void testGetKeys() throws UniversityFailure, UniversityWarning {
        MyMap<String> s = new MyMap<String>();
        s.set(key1, "1");
        s.set(key2, "2");
        s.set(key3, "3");
        String[] expected = { key1, key2, key3 };
        assertArrayEquals(expected, s.getKeys());
    }

}
